//
//  MainViewController.m
//  SinaWeibo
//
//  Created by Apple on 16/8/6.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "MainViewController.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "DiscoverViewController.h"
#import "ProfileViewController.h"
#import "MainTabar.h"
#import "ComposeViewController.h"



@interface MainViewController ()<MainTabarDelegate>

@end

@implementation MainViewController


- (void)viewDidLoad {
    [super viewDidLoad];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if (self == [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        //添加子控制器。
        [self steUpChildViewController];
        
        //替换系统原有的tabar
        MainTabar *mainTabBar = [[MainTabar alloc]init];
        
        mainTabBar.delegate = self;
        
        //    self.tabBar = mainTabar; //他是只读的 ，可以尝试用kvc来动态赋值 。
        [self setValue:mainTabBar forKey:@"tabBar"];
    }
    return self;
}


#pragma mark - MainTabarDelegate
-(void)mainTabbar:(MainTabar *)mainTabar plusBtn:(UIButton *)plusBtn{
    
    //弹出UINaviGationView
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"ComposeView" bundle:nil];
    
    ComposeViewController *composeVC = sb.instantiateInitialViewController;
    
    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:composeVC];
    
    [self presentViewController:navC animated:YES completion:nil];
    
}


#pragma mark ---加载子控制器及其方法
-(void)steUpChildViewController{
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"HomeViewController" bundle:nil];
    HomeViewController *homeVC = sb.instantiateInitialViewController;
    //    HomeViewController *homeVC = [[HomeViewController alloc]init];
    [self addChildViewController:homeVC imageName:@"tabbar_home" title:@"首页" ];
    MessageViewController *messageVC = [[MessageViewController alloc]init];
    [self addChildViewController:messageVC imageName:@"tabbar_message_center" title:@"消息"];
    DiscoverViewController *discoverVC = [[DiscoverViewController alloc]init];
    [self addChildViewController:discoverVC imageName:@"tabbar_discover" title:@"发现"];
    ProfileViewController *profileVC = [[ProfileViewController alloc]init];
    [self addChildViewController:profileVC imageName:@"tabbar_profile" title:@"我"];
    
}

/**
 *  添加子控制器
 *
 *  @param childController 要添加的子控制器
 *  @param imagename       要添加tabbar的图片名字
 *  @param title           要添加的导航控制器和tabbar下面的title;
 */
-(void)addChildViewController:(UIViewController *)childController imageName:(NSString *)imagename title:(NSString *)title{
    
    
    
    UINavigationController *navVC = [[UINavigationController alloc]initWithRootViewController:childController];
    
    childController.title = title;
    
    childController.tabBarItem.image = [UIImage imageNamed:imagename];
    
#warning 方法一
    //        设置图片渲染模式。
    NSString *selectImgName = [imagename stringByAppendingString:@"_highlighted"];
    
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectImgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    
    [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
#warning 方法二
    //渲染模式或者可以用下面这句话来代替。
    //    self.tabBar.tintColor = [UIColor orangeColor];
    
    [self addChildViewController:navVC];
    
}


@end
